//
//  CZTaskListViewController.m
//  nsj_iOS
//
//  Created by apple on 2017/1/9.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZTaskListViewController.h"
#import "CZTaskCell.h"
#import "CZTaskDetailsViewController.h"
static NSString * const task_cell = @"task_cell";

@interface CZTaskListViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *taskTableView;
@property (nonatomic, strong) NSMutableArray *taskArray;
@property (nonatomic, strong) LGRefreshView *refreshView;
@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation CZTaskListViewController

- (NSMutableArray *__nonnull)taskArray {
    if (!_taskArray) {
        _taskArray = [[NSMutableArray alloc] init];
    }
    return _taskArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [_taskTableView registerNib:[UINib nibWithNibName:@"CZTaskCell" bundle:nil] forCellReuseIdentifier:task_cell];
    _taskTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _taskTableView.estimatedRowHeight = 92;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 15)];
    headerView.backgroundColor = GLOBAL_BACKGROUND_COLOR;
    _taskTableView.tableHeaderView = headerView;
    _taskTableView.backgroundColor = GLOBAL_BACKGROUND_COLOR;
    // Do any additional setup after loading the view from its nib.
    
    _refreshView = [LGRefreshView refreshViewWithScrollView:self.taskTableView
                                             refreshHandler:^(LGRefreshView *refreshView){
                                                 [self.taskTableView.mj_footer setState:(MJRefreshStateIdle)];
                                                 _currentPage = 1;
                                                 [self getTaskListData];
                                             }];
    _refreshView.center = CGPointMake(Main_Screen_Width / 2, _refreshView.center.y);
    _refreshView.tintColor = ThemeColor;
    _refreshView.backgroundColor = [UIColor clearColor];
    
    self.taskTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _currentPage ++;
        [self getTaskListData];
    }];
    self.taskTableView.mj_footer.hidden = YES;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.taskArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CZTaskCell *cell = [tableView dequeueReusableCellWithIdentifier:task_cell];
    NSDictionary *dic = self.taskArray[indexPath.row];
    cell.taskTitleLabel.text = dic[@"task_name"];

    NSString *startTime = dic[@"start_date"];
    NSString *endTime = dic[@"end_date"];
    NSString *task_limit = dic[@"task_limit"];
    NSString *task_require = dic[@"task_require"];
    NSString *task_star = dic[@"task_star"];
    if (task_limit.integerValue < 100) {
        cell.taskTypeLabel.text = @"全员任务";
        cell.taskTypeImageView.image = [UIImage imageNamed:@"icon_nonmember"];
    } else {
        cell.taskTypeLabel.text = @"公民任务";
        cell.taskTypeImageView.image = [UIImage imageNamed:@"icon_member"];
    }
    NSString *dateTime = [[[startTime substringToIndex:10] stringByAppendingString:@"至"] stringByAppendingString:[endTime substringToIndex:10]];
    cell.taskTimeLabel.text = dateTime;
    
    if (task_require.integerValue == 0) {
        //选做
        cell.taskRightImageView.image = [UIImage imageNamed:@"icon_option"];
        cell.rightLabel.text = @"选作任务";
        cell.rightLabel.textColor = mRGBColor(56, 137, 126);
    } else {
        //必做
        cell.taskRightImageView.image = [UIImage imageNamed:@"icon_must"];
        cell.rightLabel.text = @"必须任务";
        cell.rightLabel.textColor = mRGBColor(255, 195, 0);
    }
    [cell setStarsWithNumber:task_star.integerValue];
    
    return cell;
}

- (void)getTaskListData {
    NSString *urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/task/index",APP_HOST];
    [SVProgressHUD show];
    [GCZAFNetWorking POST:urlString params:@{@"page" : @(_currentPage)} success:^(NSDictionary *response) {
        [SVProgressHUD dismiss];
        if ([response[@"code"] isEqualToString:@"200"]) {
            
            NSArray *arr = response[@"result"];
            if (_currentPage == 1) {
                self.taskArray = arr.mutableCopy;
            } else {
                for (NSDictionary *dic in arr) {
                    [self.taskArray addObject:dic];
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.taskTableView reloadData];
                self.taskTableView.mj_footer.hidden = NO;
            });
            [self.taskTableView.mj_footer endRefreshing];
        } else if ([response[@"code"] isEqualToString:@"300"]) {
            [self.taskTableView.mj_footer endRefreshingWithNoMoreData];
        }
        [_refreshView endRefreshing];
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [_refreshView endRefreshing];
        [self.taskTableView.mj_footer endRefreshing];
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = self.taskArray[indexPath.row];
    NSString *task_id = dic[@"task_id"];
    CZTaskDetailsViewController *detailVC = [[CZTaskDetailsViewController alloc] init];
    detailVC.task_id = task_id;
    [self.parentViewController.navigationController pushViewController:detailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
